package com.logictech.activity.system.service.impl;

import com.logictech.activity.system.dao.SystemAuthDao;
import com.logictech.activity.system.dao.SystemRoleDao;
import com.logictech.activity.system.entity.SystemRole;
import com.logictech.activity.system.service.SystemRoleService;
import com.logictech.lims.service.LogService;

import java.util.List;
import javax.annotation.Resource;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("systemRoleService")
public class SystemRoleServiceImpl implements SystemRoleService {
	@Resource
	private SystemRoleDao systemRoleDao;
	
	@Resource
	private SystemAuthDao systemAuthDao;
	
	@Autowired
	private LogService logService;

	public void doAdd(SystemRole systemRole) {
		this.systemRoleDao.add(systemRole);
		logService.doAdd("1", systemRole);
	}

	public void doEdit(SystemRole systemRole) {
		this.systemRoleDao.update(systemRole);
		logService.doAdd("2", systemRole);
	}

	@Transactional(rollbackFor = { Exception.class })
	public void doDelete(Long id, String roleId) {
		SystemRole systemRole = queryById(id);
		logService.doAdd("3", systemRole);
		this.systemAuthDao.deleteRoleAuthRels(roleId);
		this.systemRoleDao.delete(id);
		this.systemAuthDao.deleteRolesByRoleId(roleId);
	}

	public SystemRole queryById(Long id) {
		SystemRole systemRole = (SystemRole) this.systemRoleDao.get(id);
		return systemRole;
	}

	public PageList<SystemRole> queryPageList(PageQuery<SystemRole> pageQuery) {
		PageList<SystemRole> result = new PageList();
		Integer itemCount = this.systemRoleDao.count(pageQuery);
		List<SystemRole> list = this.systemRoleDao.queryPageList(pageQuery, itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount.intValue(), pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	public List<SystemRole> queryAllRoleList() {
		return this.systemRoleDao.queryAllRoleList();
	}
}

